var Token = require('../utils/jwtutil')
var models = require('../mysql/mysql');

/**
 * 登录接口
 * /user/login
 *  业务: 获取表单数据，验证用户是否已经注册，
 *
 */
 const loginControl = async (req, res, next) => {
	// 接收参数
	// console.log(req.body);
	const {username,password} = req.body
	var sql = `select username,password,nick,role_id,img_url,rolename from operator where username='${username}' and password='${password}'`;   // 表名，你自己数据库里有数据的表  
	let data = await models(sql)
	// console.log(data);
	// console.log(data[0].nick);
	// console.log(req._startTime.slice());

	if (data.length > 0) {
		// 登录成功
		// 记录日志 operator_log req._startTime
		var sql2 = `insert into operator_log(username,level,date,action) 
		values("${username}","${data[0].rolename}",'${req._startTime}','登录')`
		await models(sql2)
		// 1. 生成token
		let token = Token.encrypt({username, password})
		//eyJhbpJ1c2VybmFZXVCJ9.eyJ1c2VybmFZwODMzODk0fQ.AQ29Lr-c4WOPfxTajKlAME
		res.send({
			code: 1,
			info: data[0],
			token
		})
	} else {
		res.send({
			code: -1,
			info: '账号不存在!',
		})
	}
}
/**
 * 操作员列表
 * @param {*} req 
 * @param {*} res 
 * @param {*} next 
 */
const operatorList = async (req, res, next) => {
	// 接收参数
	// console.log(req.query);

	var sql = `select * from operator`; 
	let data = await models(sql)

	if (data.length > 0) {
		res.send({
			code: 1,
			info: data,
		})
	} else {
		res.send({
			code: -1,
			info: '获取失败!',
		})
	}
}
/**
 * 操作员编辑
 * @param {*} req 
 * @param {*} res 
 * @param {*} next 
 */
const operatorUpdata = async (req, res, next) => {
	// 接收参数
	const imgurl = '' + req.file.filename
	const {categoryId,product,shop,price,oldprice,putaway,id} = req.body
	console.log(req.body);
	var sql = `update operator set product="${product}",shop="${shop}",price=${price},oldprice=${oldprice},putaway=${putaway},
	category_id=${categoryId},img_url="${imgurl}" where id=${id};`

	// var sql = `delete from goods where id in(31,32)"`; 

	try {
		await models(sql)
		res.send({
			code: 1,
			info: '操作员编辑成功',
		})
	} catch (eror) {
		res.send({
			code: -1,
			info: '操作员编辑失败',
		})
	}
}

// /**
//  * 删除操作员
//  * /goods/deleteorder
//  * /goods/1
//  * delete
//  *  post请求参数 req.body
//  *
//  */
const delteOperatorById = async (req, res, next) => {
	// 接收参数
	console.log(req.body);
	const {id} = req.body
	var sql = `delete from operator where id="${id}"`; 

	try {
		await models(sql)
		res.send({
			code: 1,
			info: '删除成功',
		})
	} catch (eror) {
		res.send({
			code: -1,
			info: '删除失败',
		})
	}
}
/**
 * 添加操作员
 * @param {*} req 
 * @param {*} res 
 * @param {*} next 
 */
const addOperator = async (req, res, next) => {
	// 接收参数
	const imgurl = '' + req.file.filename
	const {categoryId:roleId,product:nick,shop:username,price:password} = req.body
	// console.log(imgurl);
	console.log(req.body);

	var sql = `insert into operator(username,password,nick,role_id,img_url) 
	values("${username}","${password}",'${nick}','${roleId}','${imgurl}')`; 
	// var sql = `insert into operator(username,password,nick,role_id,img_url) 
	// values("gao","zxczxc123","普通管理员","2",'${imgurl}')`; 


	try {
		await models(sql)
		res.send({
			code: 1,
			info: '操作员添加成功',
		})
	} catch (eror) {
		res.send({
			code: -1,
			info: '操作员添加失败',
		})
	}
}

/**
 * 登录日志接口
 * /operator/log
 *  业务: 获取表单数据，验证用户是否已经注册，
 *
 */
 const logControl = async (req, res, next) => {
	// 接收参数
	var sql = `select * from operator_log`;   // 表名，你自己数据库里有数据的表  
	let data = await models(sql)

	if (data.length > 0) {
		res.send({
			code: 1,
			info: data.reverse(),
		})
	} else {
		res.send({
			code: -1,
			info: '获取失败!',
		})
	}
}



module.exports = {loginControl,logControl,operatorList,operatorUpdata,addOperator,delteOperatorById}